
function try2ShowMiniCoupon(from) {
	/*
		wuchen/user_unique_code.php?action=inmarketing
		返回inmarketing:true or false

		查询用户券的状态[GET]
		wuchen/user_unique_code.php?action=userstatus&uid=xxx
	*/
	var q = {
        action: 'inmarketing',
        from: from || ''
    };
	ajax_get(cfg.uni_code_api, q, {
		succ: function(json) {
			if (true == json.inmarketing) {
				var q2 = {
					action: 'userstatus',
					uid: app.auth.getMyId(),
				};
				ajax_get(cfg.uni_code_api, q2, {
					succ: function(json) {
						// 未领取，或领取了但未使用，则显示入口
						var st = json.status;
						window.uniCodeStatus = st;
						if ('hasnocode' == st || 'normal' == st) {
							doShowMiniCoupon();
						} else {
							tlog('NOT show mini-coupon. unique code status: ' + st);
                            rmMiniCoupon();
						}
					},
					fail: function(err, url) {
						utils.postErrWithTips(err, url);
					}
				});
			} else {
				tlog('NOT in markting time, so not show coupon');
                rmMiniCoupon();
			}
		}
	});
}

function doShowMiniCoupon() {
	wlog('show mini coupon');
	var el = $api.byId('mini-coupon');
    if (!el) {
        var html = doT_Render('#tpl-mini-coupon', {});
        $api.append($api.dom('body'), html)
        sofast(); // 优化 tapmode
    }
}

function rmMiniCoupon() {
	var el = $api.byId('mini-coupon');
    $api.remove(el);
}

function try2GetCoupon(el) {
	if(hasPhone()){
		switch (window.uniCodeStatus) {
			case 'hasnocode':
				views.openCouponAdWin();
				break;
			case 'normal':
				views.openCouponQrWin();
				break;
			default:
				wlog('no way');
		}
	}
}

function listenToUpdateUniCode() {
	evt.listenTo('update-uni-code', function(data) {
		window.uniCodeStatus = data.status;

		// 一旦劵用过，就关闭入口
		if ('used' == data.status) {
			rmMiniCoupon();
		}
	});
}

function fooCoupon() {
	doShowMiniCoupon();
}
